import appStore from '@/store/index'
import { storeToRefs } from 'pinia'
import type { Router } from 'vue-router'
const whiteList = ['/login']

// 注册路由守卫
export default function registerEach(router: Router) {
  router.beforeEach((to, from, next) => {
    const { haveToken, getInfo } = appStore.useUserStore
    if (haveToken()) {
      const { isLogin } = storeToRefs(appStore.useUserStore)
      if (isLogin.value) return next()
      getInfo().then(() => {
        next()
      })
    } else {
      if (whiteList.includes(to.path)) {
        next()
      } else {
        next('/login')
      }
    }
  })
}
